<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2020-05-07
 */

namespace app\api\model\v1;

use think\Db;
use think\Cache;
use think\Config;

/**
 * 分销商模型
 */
load_trait('controller/Jump');

class Dealer extends UserBase
{
    use \traits\controller\Jump;

    //初始化
    protected function initialize()
    {
        // 需要调用`Model`的`initialize`方法
        parent::initialize();
        // 分销商数据信息表
        $this->dealerUsers = Db::name('dealer_users');
        // 分销商申请表
        $this->dealerApply = Db::name('dealer_apply');
        // 分销商关系层级表
        $this->dealerRelation = Db::name('dealer_relation');
    }

    // 查询分销商申请信息
    public function queryDealerApply()
    {
        $where = [
            'users_id' => $this->users_id
        ];
        $dealerApply = $this->dealerApply->where($where)->find();
        if (empty($dealerApply) || (!empty($dealerApply) && in_array($dealerApply['review_status'], [1, 3]))) {
            $result = [
                'allowApply' => 1,
                'jumpPage' => '/pages/dealer/apply/apply',
            ];
            $this->success('允许申请', null, $result);
        }
        // dump($dealerApply);
        // exit;
    }

    // 获取分销商申请
    public function getDealerApply($param = [], $users = [])
    {
        // 分销商申请数据
        $result = [
            'action' => 'addApply',
            'mobile' => !empty($users['mobile']) ? $users['mobile'] : 0,
            'nickname' => !empty($users['nickname']) ? $users['nickname'] : $users['username'],
            'dealer_mobile' => !empty($users['mobile']) ? $users['mobile'] : 0,
            'dealer_name' => !empty($users['nickname']) ? $users['nickname'] : $users['username'],
        ];

        // 如果存在推荐人则执行
        if (!empty($param['referrer_id'])) {
            $where = [
                'dealer_id' => $param['referrer_id']
            ];
            $dealerUsers = $this->dealerUsers->where($where)->find();
        } else {
            $result['referrer_id'] = 0;
            $result['referrer_name'] = '平台';
        }

        // 查询是否已提交申请
        $where = [
            'users_id' => $this->users_id
        ];
        $apply = $this->dealerApply->where($where)->find();
        if (!empty($apply)) {
            if (!empty($apply['review_status']) && 2 === intval($apply['review_status'])) {
                $result = [
                    'code' => 'success',
                    'jumpPage' => '/pages/dealer/index/index'
                ];
                $this->success('申请通过', null, $result);
            } else if (!empty($apply['review_status']) && 3 === intval($apply['review_status'])) {
                $apply['review_reason'] = '申请被拒接，原因：' . $apply['review_reason'];
                $result = array_merge($apply, $result);
            } else if (!empty($apply['review_status']) && 1 === intval($apply['review_status'])) {
                $apply['review_reason'] = '正在审核中，请耐心等待..';
                $result = array_merge($apply, $result);
            }
        }
        // dump($result);exit;

        // 返回数据
        $this->success('查询成功', null, $result);
    }

    // 获取分销商申请
    public function addDealerApply($param = [], $users = [])
    {
        // 分销商名称及分销商手机号码
        if (empty($param['dealer_name']) || empty($param['dealer_mobile'])) $this->error('请填写申请信息');

        // 申请分销商数据
        $times = getTime();
        $insert = [
            'users_id' => $this->users_id,
            'referrer_id' => !empty($param['referrer_id']) ? intval($param['referrer_id']) : 0,
            'dealer_name' => !empty($param['dealer_name']) ? strval($param['dealer_name']) : '',
            'dealer_mobile' => !empty($param['dealer_mobile']) ? strval($param['dealer_mobile']) : '',
            'add_time' => $times,
            'update_time' => $times,
        ];
        $applyID = $this->dealerApply->insertGetId($insert);
        if (!empty($applyID)) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }
    }
}